﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using FlanProjectData.Characters;
using FlanProjectData.GameObjects;
using FlanProjectData.Helpers;
using FlanProjectData.Sprites;

namespace FlanProjectData.GameObjects
{
    public class Party
    {
        private static Logger logger = new Logger("Party", Log_Type.DEBUG);
        private String nameSave;

        [ContentSerializerIgnore]
        public List<Player> PlayerList { get; set; }
        public float MovementSpeed { get; set; }
        [ContentSerializerIgnore]
        public Vector2 Position { get; set; }
        [ContentSerializerIgnore]
        public Inventory inventory { get; set; } // inventaire, commun à toute l'equipe

        [ContentSerializerIgnore]
        public Quest currentQuest { get; set; } // Liste des quetes que le joueur a à faire
        [ContentSerializerIgnore]
        public List<Quest> questDone { get; set; }	// Contient les quetes déjà réalisé par le joueur
		[ContentSerializerIgnore]
		public int NextQuest { get; set; }


        #region Animation

        public Animation MoveUp { get; set; }
        public Animation MoveDown { get; set; }
        public Animation MoveLeft { get; set; }
        public Animation MoveRight { get; set; }
        public Animation Standby { get; set; }

        #endregion

        public Party()
        {
            logger.Debug("on crée l'equipe");
            PlayerList = new List<Player>();
            Position = Vector2.Zero;
            this.questDone = new List<Quest>();
            //this.inventory = new Inventory();
        }

        public void newGame()//on charge la Team à partir d'un fichier par default
        {

        }
        public void newGameLoading(String name)// on charge la Team à partir d'un fichier de sauvegarde 
        {
            logger.Debug("nom de la sauvegarde: " + name);
            this.nameSave = name;
        }

        public void creationSauvegarde(ContentManager cm, String chemin)
        {
            logger.Debug("chemin: " + cm.RootDirectory);
            logger.Debug("chemin de la sauvegarde: " + cm.RootDirectory + "/Saves/" + chemin);

            foreach (var player in cm.Load<List<String>>(@chemin))
            {
                // TODO : Players
                var newPlayer = cm.Load<Player>(@"Data/Characters/Players/" + player);
                PlayerList.Add(newPlayer);
            }

            logger.Debug("combien de perso:  " + PlayerList.Count);
        }



        public void LoadContent(ContentManager cm)
        {
            /*
            logger.Debug("chemin: " + cm.RootDirectory);
            logger.Debug("chemin de la sauvegarde: " + cm.RootDirectory + "/Saves/"+this.nameSave+"/save");
            PlayerList = cm.Load<List<Player>>(@"Saves/"+this.nameSave+"/save");
            logger.Debug("combien de perso:  " + PlayerList.Count);
            */

            try
            {
                this.MoveDown.LoadContent(cm);
                this.MoveLeft.LoadContent(cm);
                this.MoveRight.LoadContent(cm);
                this.MoveUp.LoadContent(cm);
                this.Standby.LoadContent(cm);
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
            }
        }

        public void save(string name)
        {

            StreamWriter leStream = new StreamWriter("Content/Saves/" + name + "/save.xnb");
            XmlSerializer leSerial = new XmlSerializer(typeof(Party));
            leSerial.Serialize(leStream, this);
            leStream.Close();

        }

        public void addItemInventory(Item newItem)
        {
            this.inventory.AdItem(newItem);
        }
        public void addItemInventoryAt(int i, Item newItem)
        {
            this.inventory.setItemAt(i, newItem);
        }
        public bool ThisQuestIsEverDone(int id)
        {
            bool ret = false;
            int i = 0;

            while (!ret && i < this.questDone.Count)
            {
                if (this.questDone[i].IdQuest == id)
                    ret = true;
                i++;
            }

            return ret;
        }
        public void addItemToCharacter(int indexItem, int indexPlayer) // A TESTER Permet de donné un objet à un membre de l'equipe. L'ancien objet est mis dans l'inventaire
        {
            if (indexPlayer > this.PlayerList.Count)
                logger.Error("Erreur, personnage inexistant à cette index");
            else
            {
                // on ajoute le nouvelle item a la place de l'ancien. L'inventaire est modifié en consequence.
                addItemInventoryAt(indexItem, this.PlayerList[indexPlayer].addItemEquipement(this.inventory.lstItems[indexItem]));
            }
        }
    }
}
